package com.qiao.train.controller;

import com.qiao.train.DAO.PersonDAO;
import com.qiao.train.po.Person;
import com.qiao.train.config.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class PersonController {
    @Autowired

    private PersonDAO MyPersonDAO;

    @PostMapping("/login")
    public ResponseResult login(String name, String psd) {
        Person data = MyPersonDAO.login(name, psd);
        return data != null ? ResponseResult.okResult(200, "登录成功", data) : ResponseResult.okResult(201, "登录失败", null);
    }

    @GetMapping("/getAllUser")
    public List<Person> getAllUser() {
        return MyPersonDAO.getAllUser();
    }

    @GetMapping("/getUserById")
    public Person getUserById(int id) {
        return MyPersonDAO.getUserById(id);
    }

    @GetMapping("/createUser")
    public int createUser(Person user) {
        return MyPersonDAO.createUser(user);
    }

    @GetMapping("/deleteUser")
    public int deleteUser(int id) {
        return MyPersonDAO.deleteUser(id);
    }

    @GetMapping("/updateUser")
    public int updateUser(Person user) {
        return MyPersonDAO.updateUser(user);
    }
}
